﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MShop.Core.DataAccess;
using MShop.Core.Domain;
using MShop.Web.Components.Paging;
using MShop.Core;
using MShop.Web.Components.Extensions;
using MShop.Core.Util;

namespace MShop.Web.Areas.Admin.Controllers
{
    public class ItemPropController : SecureController
    {
        private readonly IItemPropDao _ItemPropDao;
        private readonly IItemCatDao _ItemCatDao;
        private readonly ICommonResolver _Resolver;
        public ItemPropController(IItemPropDao itemPropDao, IItemCatDao itemCatDao,ICommonResolver resolver)
        {
            _Resolver = resolver;
            _ItemCatDao = itemCatDao;
            _ItemPropDao = itemPropDao;
        }
        public ActionResult Index(int? page)
        { 
            var query = _ItemPropDao.Build();  
            return View(query);
        }

        public ActionResult Create()
        {
         
            ViewData["selectlist"] = _ItemPropDao.BuildSelectList(null,true);
            ViewData["ItemPropType"] = typeof(ItemPropType).ConvertListItem();
            return View(new ItemProp());
        }

        [HttpPost]
        public ActionResult Create(ItemProp itemprop)
        {
            string props = Request.Form["PropId"];
          
            if (ModelState.IsValid)
            {
                if (itemprop.ItemPropType == ItemPropType.Enum)
                {
                    string[] values = itemprop.PropValues.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                    itemprop.IsEnumProp = true;
                    foreach (string value in values)
                    {
                        ItemPropValue propv = new ItemPropValue()
                        {
                            ItemPropId = itemprop.Id,
                            PropValue = value
                        };
                        itemprop.ItemPropValue.Add(propv);
                    }
                }
                else
                {
                    itemprop.IsInputProp = true;
                }

                switch (props)
                {
                    case "1":
                        itemprop.IsKeyProp = true;
                        itemprop.Must = true;
                        break;
                    case "2":
                        itemprop.IsItemProp = true;
                        itemprop.Must =false;
                        break;
                    case "3":
                        itemprop.IsSaleProp = true;
                        itemprop.Must = true;
                        break;
                } 
                _ItemPropDao.Save(itemprop);
                _ItemPropDao.SaveChanges();
                return RedirectToAction("Index");
            } 
            ViewData["ItemPropType"] = typeof(ItemPropType).ConvertListItem(itemprop.PropType);
            ViewData["selectlist"] = _ItemPropDao.BuildSelectList(itemprop.ParentId, true);
            return View(itemprop);
        }

        public ActionResult Delete(int id)
        {
            var model=_ItemPropDao.GetById(id);
            _ItemPropDao.Delete(model.ItemPropValue);
            _ItemPropDao.Delete(model);
            _ItemPropDao.SaveChanges();
            return RedirectToAction("Index");
        }


        public ActionResult Change(int id)
        {
          
            var model = _Resolver.GetCommon<ItemProp>().GetById(id);
            ViewData["selectlist"] = _ItemPropDao.BuildSelectList(model.ParentId, true);
            ViewData["ItemPropType"] = typeof(ItemPropType).ConvertListItem();
            return View(model);
            
        }
        [HttpPost]
        public ActionResult Change(int id, ItemProp itemprop)
        {
            var model = _Resolver.GetCommon<ItemProp>().GetById(id);
            this.TryUpdateModel<ItemProp>(model, new string[] { });
           _Resolver.GetCommon<ItemProp>().SaveChanges();
            return RedirectToAction("Index");

        }
    }
}
